package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.dudu.model.AppExtend;

import core.CoreDao;
import core.db.DBConnectionUtil;

/**
 * App扩展信息数据库操作类
 * 
 * @author 危东
 * @date 2016-4-19
 */
public class AppExtendDao {

	// 日志输出
	private static final Logger log = Logger.getLogger(AppExtendDao.class.getSimpleName());

	/**
	 * 新增app扩展信息
	 * 
	 * @param coreDao
	 * @param appExtend
	 * @return
	 */
	public int insertAppExtend(CoreDao coreDao, AppExtend appExtend) {
		log.info("AppExtendDao.insertAppExtend");
		// 数据库连接
		Connection con = null;
		// 最终返回结果
		int result = -1;
		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_app_extend(app_key, periphery_code, key_public, key_private, "
							+ "linker_name, linker_phone, linker_email," + "is_init_url,is_init_validate,"
							+ " url_ticket, url_callstatus, url_authentication,"
							+ " url_ivrdigit,url_notice, url_record,url_sms, billing_platform, create_time, create_user,url_infocontrol) " +
							"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate(),?,?)");
			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(),
					new Object[] { appExtend.getApp_key(), appExtend.getPeriphery_code(), appExtend.getKey_public(),
							appExtend.getKey_private(), appExtend.getLinker_name(), appExtend.getLinker_phone(),
							appExtend.getLinker_email(), appExtend.getIs_init_url(), appExtend.getIs_init_validate(),
							appExtend.getUrl_ticket(), appExtend.getUrl_callstatus(), appExtend.getUrl_authentication(),
							appExtend.getUrl_ivrdigit(), appExtend.getUrl_notice(), appExtend.getUrl_record(),
							appExtend.getUrl_sms(), appExtend.getBilling_platform(), appExtend.getCreate_user(),appExtend.getUrl_infocontrol() });
			// 获取插入的结果
			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.error("AppExtendDao.insertAppExtend 插入应用扩展信息表出错：" + e);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 修改app扩展信息
	 * 
	 * @param coreDao
	 * @param appExtend
	 * @return
	 */
	public int updateAppExtend(CoreDao coreDao, AppExtend appExtend) {
		log.info("AppExtendDao.updateAppExtend");
		// 数据库连接
		Connection con = null;
		// 最终返回结果
		int result = -1;
		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer(
					" UPDATE t_app_extend set app_key=?,periphery_code=?," + "key_public=?,key_private=?,"
							+ "linker_name=?,linker_phone=?,linker_email=?," + "is_init_url=?,is_init_validate=?,"
							+ "url_ticket=?,url_callstatus=?,url_authentication=?,url_ivrdigit=?,url_notice=?, url_record=?,url_sms=?,"
							+ "billing_platform=?,create_time=sysdate(),create_user=?,url_infocontrol=?");
			sql.append(" where id=?");

			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(),
					new Object[] { appExtend.getApp_key(), appExtend.getPeriphery_code(), appExtend.getKey_public(),
							appExtend.getKey_private(), appExtend.getLinker_name(), appExtend.getLinker_phone(),
							appExtend.getLinker_email(), appExtend.getIs_init_url(), appExtend.getIs_init_validate(),
							appExtend.getUrl_ticket(), appExtend.getUrl_callstatus(), appExtend.getUrl_authentication(),
							appExtend.getUrl_ivrdigit(), appExtend.getUrl_notice(), appExtend.getUrl_record(),
							appExtend.getUrl_sms(), appExtend.getBilling_platform(), appExtend.getCreate_user(),
							appExtend.getUrl_infocontrol(), appExtend.getId() });
			// 获取插入的结果
			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("AppExtendDao.updateAppExtend 插入应用扩展信息表出错：" + e);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 根据app标识查询app扩展信息
	 * 
	 * @param appIdentify
	 * @return
	 */
	public AppExtend queryAppExtendByIdentify(CoreDao coreDao, String appIdentify) {
		log.info("AppExtendDao.queryAppExtendByIdentify");
		// 数据库连接
		Connection con = null;
		// 最终返回结果
		AppExtend result = null;

		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer("SELECT * FROM t_app_extend  where app_key=?");
			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(), new Object[] { appIdentify });
			// 类型转换
			if (list != null && list.size() > 0) {
				// 类型转换
				List<AppExtend> items = coreDao.covertToBean(list, AppExtend.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("根据app标识查询App扩展信息出错：" + ex);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 查询条数（分页用）
	 * 
	 * @param paramSql
	 * @return
	 */
	public int getTotalAcount(CoreDao dao, String paramSql) {
		if (StringUtils.isNotBlank(paramSql)) {
			paramSql = " and " + paramSql;
		}
		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select count(1) as totalAcount " + " from t_app_extend" + " where 1=1  " + paramSql);

			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(), new Object[] {});

			JSONObject obj = list.get(0);
			result = (Integer) obj.get("totalAcount");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 查询数据（分页）
	 * 
	 * @param paramSql
	 * @param pageSql
	 * @return
	 */
	public List<JSONObject> queryAll(CoreDao dao, String paramSql, String pageSql) {

		Connection con = null;
		List<JSONObject> result = null;
		if (StringUtils.isNotBlank(paramSql)) {
			paramSql = " and " + paramSql;
		}

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select *  from t_app_extend where 1=1" + paramSql + " order by create_time desc " + pageSql);
			result = dao.serviceJsonSql(con, sql.toString(), new Object[] { null });

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
}
